





<!DOCTYPE html>
<html class="writer-html5" lang="zh-CN" >
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>代码指南和提示 &mdash; tvm 0.8.dev1982 文档</title>
  

  
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/gallery.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tlcpack_theme.css" type="text/css" />

  
  
    <link rel="shortcut icon" href="../_static/tvm-logo-square.png"/>
  

  
  
  
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
        <script src="../_static/jquery.js"></script>
        <script src="../_static/underscore.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    
    <script type="text/javascript" src="../_static/js/tlcpack_theme.js"></script>
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="Error Handling Guide" href="error_handling.html" />
    <link rel="prev" title="Write Documentation for TVM" href="document.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    
<header class="header">
    <div class="innercontainer">
      <div class="headerInner d-flex justify-content-between align-items-center">
          <div class="headerLogo">
               <a href="https://tvm.apache.org/"><img src=https://tvm.apache.org/assets/images/logo.svg alt="logo"></a>
          </div>

          <div id="headMenu" class="headerNav">
            <button type="button" id="closeHeadMenu" class="navCloseBtn"><img src="../_static/img/close-icon.svg" alt="Close"></button>
             <ul class="nav">
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/community>Community</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/download>Download</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/vta>VTA</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/blog>Blog</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/docs>Docs</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvmconf.org>Conference</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://github.com/apache/tvm/>Github</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvmchinese.github.io/declaration_zh_CN.html>About-Translators</a>
                </li>
             </ul>
               <div class="responsivetlcdropdown">
                 <button type="button" class="btn-link">
                   ASF
                 </button>
                 <ul>
                     <li>
                       <a href=https://apache.org/>Apache Homepage</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/licenses/>License</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/foundation/sponsorship.html>Sponsorship</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/security/>Security</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/foundation/thanks.html>Thanks</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/events/current-event>Events</a>
                     </li>
                     <li>
                       <a href=https://www.zhihu.com/column/c_1429578595417563136>Zhihu</a>
                     </li>
                 </ul>
               </div>
          </div>
            <div class="responsiveMenuIcon">
              <button type="button" id="menuBtn" class="btn-menu"><img src="../_static/img/menu-icon.svg" alt="Menu Icon"></button>
            </div>

            <div class="tlcDropdown">
              <div class="dropdown">
                <button type="button" class="btn-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  ASF
                </button>
                <div class="dropdown-menu dropdown-menu-right">
                  <ul>
                     <li>
                       <a href=https://apache.org/>Apache Homepage</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/licenses/>License</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/foundation/sponsorship.html>Sponsorship</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/security/>Security</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/foundation/thanks.html>Thanks</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/events/current-event>Events</a>
                     </li>
                     <li>
                       <a href=https://www.zhihu.com/column/c_1429578595417563136>Zhihu</a>
                     </li>
                  </ul>
                </div>
              </div>
          </div>
       </div>
    </div>
 </header>
 
    <nav data-toggle="wy-nav-shift" class="wy-nav-side fixed">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html">
          

          
            
            <img src="../_static/tvm-logo-small.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
                <div class="version">
                  0.8.dev1982
                </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption" role="heading"><span class="caption-text">如何开始</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../install/index.html">安装 TVM</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">贡献者指南</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="community.html">TVM社区指南</a></li>
<li class="toctree-l2"><a class="reference internal" href="code_review.html">执行代码审查</a></li>
<li class="toctree-l2"><a class="reference internal" href="committer_guide.html">Committer Guide</a></li>
<li class="toctree-l2"><a class="reference internal" href="document.html">Write Documentation for TVM</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">代码指南和提示</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#c-code-styles">C++ 代码规范</a></li>
<li class="toctree-l3"><a class="reference internal" href="#python-code-styles">Python代码规范</a></li>
<li class="toctree-l3"><a class="reference internal" href="#writing-python-tests">编写Python测试代码</a></li>
<li class="toctree-l3"><a class="reference internal" href="#handle-integer-constant-expression">处理整数常量表达式</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="error_handling.html">Error Handling Guide</a></li>
<li class="toctree-l2"><a class="reference internal" href="pull_request.html">Submit a Pull Request</a></li>
<li class="toctree-l2"><a class="reference internal" href="git_howto.html">Git 使用技巧</a></li>
<li class="toctree-l2"><a class="reference internal" href="release_process.html">Apache TVM Release Process</a></li>
</ul>
</li>
</ul>
<p class="caption" role="heading"><span class="caption-text">用户引导</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">User Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../how_to/index.html">How To Guides</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">开发者引导</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../dev/tutorial/index.html">Developer Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dev/how_to/how_to.html">开发者指南</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">架构指南</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../arch/index.html">Design and Architecture</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">主题引导</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../topic/microtvm/index.html">microTVM：裸机使用TVM</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topic/vta/index.html">VTA: Versatile Tensor Accelerator</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">参考指南</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../reference/langref/index.html">语言参考</a></li>
<li class="toctree-l1"><a class="reference internal" href="../reference/api/python/index.html">Python API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../reference/api/links.html">Other APIs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../reference/publications.html">Publications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../genindex.html">索引</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
      
      <nav class="wy-nav-top" aria-label="top navigation" data-toggle="wy-nav-top">
        
            <div class="togglemenu">

            </div>
            <div class="nav-content">
              <!-- tvm -->
              Table of content
            </div>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        

          




















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html">Docs</a> <span class="br-arrow">></span></li>
        
          <li><a href="index.html">贡献者指南</a> <span class="br-arrow">></span></li>
        
      <li>代码指南和提示</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
            <a href="../_sources/contribute/code_guide.rst.txt" rel="nofollow"> <img src="../_static//img/source.svg" alt="viewsource"/></a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="code-guide-and-tips">
<span id="code-guide"></span><h1>代码指南和提示<a class="headerlink" href="#code-guide-and-tips" title="永久链接至标题">¶</a></h1>
<p>本文档用于为审阅者和贡献者在TVM代码库中记录提示。其中大部分都是从贡献和审核过程中的教训里总结得到。</p>
<div class="section" id="c-code-styles">
<h2>C++ 代码规范<a class="headerlink" href="#c-code-styles" title="永久链接至标题">¶</a></h2>
<ul class="simple">
<li><p>使用Google C/C++ 规范</p></li>
<li><p>面向大众的函数按照Doxygen格式进行记录</p></li>
<li><p>尽量使用简短的具体类型声明而不是``auto``</p></li>
<li><p>尽量通过常量引用传递（例如``const Expr&amp;``）而不是数值传递。除非在函数通过复制构造函数或移动来使用数值时，数值传递才比常量引用传递更好。</p></li>
<li><p>尽量使用 <a href="#id1"><span class="problematic" id="id2">``</span></a>const``成员函数</p></li>
</ul>
<p>我们使用`clang-format`来确保代码规范。因为不同版本的clang-format可能会有差异，我们推荐使用和主分支相同版本的clang-format。您也可以通过docker使用如下指令。</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>docker/bash.sh tlcpack/ci-lint clang-format-10 <span class="o">[</span>path-to-file<span class="o">]</span>
</pre></div>
</div>
<p>clang-format也不是完美的，如有必要，您也可以在特定代码区域不使用clang-format。</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="c1">// clang-format off</span>
<span class="kt">void</span> <span class="nf">Test</span><span class="p">()</span> <span class="p">{</span>
   <span class="c1">// clang-format will be disabled in this region.</span>
<span class="p">}</span>
<span class="c1">// clang-format on</span>
</pre></div>
</div>
<p>因为clang-format可能无法识别宏，因此我们推荐按照普通代码规范使用宏。</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#define MACRO_IMPL { custom impl; }</span>
<span class="cp">#define MACRO_FUNC(x)</span>

<span class="c1">// not preferred, because clang-format might recognize it as types.</span>
<span class="n">virtual</span> <span class="kt">void</span> <span class="nf">Func1</span><span class="p">()</span> <span class="n">MACRO_IMPL</span>

<span class="c1">// preferred</span>
<span class="n">virtual</span> <span class="kt">void</span> <span class="n">Func2</span><span class="p">()</span> <span class="n">MACRO_IMPL</span><span class="p">;</span>

<span class="kt">void</span> <span class="nf">Func3</span><span class="p">()</span> <span class="p">{</span>
  <span class="c1">// preferred</span>
  <span class="n">MACRO_FUNC</span><span class="p">(</span><span class="n">xyz</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="python-code-styles">
<h2>Python代码规范<a class="headerlink" href="#python-code-styles" title="永久链接至标题">¶</a></h2>
<ul class="simple">
<li><p>函数和类的规范记录在`numpydoc &lt;<a class="reference external" href="https://numpydoc.readthedocs.io/en/latest/">https://numpydoc.readthedocs.io/en/latest/</a>&gt;`_ 中.</p></li>
<li><p>通过``make pylint``检查您的代码规范。</p></li>
<li><p>和``python 3.6``的语言特性保持一致</p></li>
</ul>
</div>
<div class="section" id="writing-python-tests">
<h2>编写Python测试代码<a class="headerlink" href="#writing-python-tests" title="永久链接至标题">¶</a></h2>
<p>我们使用 <a href="#id1"><span class="problematic" id="id2">`</span></a>pytest &lt;<a class="reference external" href="https://docs.pytest.org/en/stable/">https://docs.pytest.org/en/stable/</a>&gt;`_进行所有的python测试。<a href="#id3"><span class="problematic" id="id4">``</span></a>tests/python``被包含在所有的测试中。</p>
<p>如果您希望您的测试能在各种目标环境下运行，请使用:py:func:<a href="#id1"><span class="problematic" id="id2">`</span></a>tvm.testing.parametrize_targets`装饰器。例如：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="nd">@tvm.testing.parametrize_targets</span>
<span class="k">def</span> <span class="nf">test_mytest</span><span class="p">(</span><span class="n">target</span><span class="p">,</span> <span class="n">dev</span><span class="p">):</span>
  <span class="o">...</span>
</pre></div>
</div>
<p>将会使用``target=”llvm”<code class="docutils literal notranslate"><span class="pre">，``target=&quot;cuda&quot;``和一些其它参数运行``test_mytest</span></code>。这也通过CI保证您的测试运行在正确的硬件上。如果您只希望针对几个环境进行测试，请使用``&#64;tvm.testing.parametrize_targets(“target_1”, “target_2”)``。如果您希望在单个场景下进行测试，请使用 :py:func:<a href="#id1"><span class="problematic" id="id2">`</span></a>tvm.testing`里的相关的装饰器。例如，CUDA测试使用``&#64;tvm.testing.requires_cuda``装饰器。</p>
</div>
<div class="section" id="handle-integer-constant-expression">
<h2>处理整数常量表达式<a class="headerlink" href="#handle-integer-constant-expression" title="永久链接至标题">¶</a></h2>
<p>我们在TVM中经常需要处理整数常量表达式。在这样做之前，我们想要问的第一个问题是，是否真的有必要得到一个整数常量。如果符号表达式也有效并符合逻辑，我们应该尽量使用符号表达式。因此生成的代码适用于事先未知的形状。</p>
<p>注意在某些情况下我们不能获取特定信息，例如符号变量的符号，因此在某些情况下可以做出假设。但如果变量是常量，则需要添加精准支持。</p>
<p>如果我们必须获得整数常量表达式，我们应该使用类型``int64_t``的常量值而不是``int``的，来避免潜在的整数溢出。我们总可以通过``make_const``用相应的表达式类型重新构建一个整数。下列代码提供了一个示例。</p>
<div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">Expr</span> <span class="nf">CalculateExpr</span><span class="p">(</span><span class="n">Expr</span> <span class="n">value</span><span class="p">)</span> <span class="p">{</span>
  <span class="kt">int64_t</span> <span class="n">int_value</span> <span class="o">=</span> <span class="n">GetConstInt</span><span class="o">&lt;</span><span class="kt">int64_t</span><span class="o">&gt;</span><span class="p">(</span><span class="n">value</span><span class="p">);</span>
  <span class="n">int_value</span> <span class="o">=</span> <span class="n">CalculateExprInInt64</span><span class="p">(</span><span class="n">int_value</span><span class="p">);</span>
  <span class="k">return</span> <span class="n">make_const</span><span class="p">(</span><span class="n">value</span><span class="p">.</span><span class="n">type</span><span class="p">(),</span> <span class="n">int_value</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
</div>


           </div>
           
          </div>
          

<footer>

    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="error_handling.html" class="btn btn-neutral float-right" title="Error Handling Guide" accesskey="n" rel="next">下一个 <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="document.html" class="btn btn-neutral float-left" title="Write Documentation for TVM" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> 上一个</a>
      
    </div>

<div id="button" class="backtop"><img src="../_static//img/right.svg" alt="backtop"/> </div>
<section class="footerSec">
    <div class="footerHeader">
      <ul class="d-flex align-md-items-center justify-content-between flex-column flex-md-row">
        <li class="copywrite d-flex align-items-center">
          <h5 id="copy-right-info">© 2020 Apache Software Foundation | All right reserved</h5>
        </li>
      </ul>

    </div>

    <ul>
      <li class="footernote">Copyright © 2020 The Apache Software Foundation. Apache TVM, Apache, the Apache feather, and the Apache TVM project logo are either trademarks or registered trademarks of the Apache Software Foundation.</li>
    </ul>

</section>
</footer>
        </div>
      </div>

    </section>

  </div>
  

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

  </body>
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-75982049-2', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>